package com.example.mobile2021606144ywr0525;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private EditText editMainUsername;
    private EditText editMainPsd;
    private CheckBox chkRmbPsd;
    private CheckBox chkMainLog;
    private Button btnMainLog;
    private Button btnMainReg;
    private String url="http://121.9.253.237:8888/traffic/api/v2/user_login";
    OkHttpClient okHttpClient=new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName=editMainUsername.getText().toString().trim();
                String passWord=editMainPsd.getText().toString().trim();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JsonObject jsonObject=new JsonObject();
                        jsonObject.addProperty("UserName",userName);
                        jsonObject.addProperty("UserPwd",passWord);
                        String json=jsonObject.toString();

                        MediaType mediaType=MediaType.Companion.parse("application/json;charset=utf-8");
                        RequestBody requestBody=RequestBody.create(json,mediaType);
                        Request request=new Request.Builder()
                                .url(url)
                                .post(requestBody)
                                .build();

                        Call call=okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result=response.body().string();
                                Gson gson=new Gson();
                                LogininBean logininBean=gson.fromJson(result,LogininBean.class);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (logininBean.getRESULT().equals("S")){
                                            new AlertDialog.Builder(MainActivity.this)
                                                    .setMessage("登陆成功")
                                                    .create()
                                                    .show();
                                        }else if (logininBean.getRESULT().equals("F")){
                                            new AlertDialog.Builder(MainActivity.this)
                                                    .setMessage("登陆失败")
                                                    .create()
                                                    .show();
                                        }
                                    }
                                });
                                Log.i("TAG","onResponse"+logininBean.getRESULT());

                            }
                        });

                    }
                }).start();
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        editMainUsername = (EditText) findViewById(R.id.edit_main_username);
        editMainPsd = (EditText) findViewById(R.id.edit_main_psd);
        chkRmbPsd = (CheckBox) findViewById(R.id.chk_rmb_psd);
        chkMainLog = (CheckBox) findViewById(R.id.chk_main_log);
        btnMainLog = (Button) findViewById(R.id.btn_main_log);
        btnMainReg = (Button) findViewById(R.id.btn_main_reg);
    }
}